<template>
  <div class="app-container">
    <label class="title" style="font-size: 20px;color: rgba(0,0,0,.85);font-weight: 600;line-height: 32px">项目主机</label>
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="98px">
      <el-form-item label="名称或地址：" prop="main">
        <el-input
          v-model="queryParams.main"
          placeholder="名称或地址"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="" clearable size="small">
          <el-option label="" value="" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:host:add']"
        >添加主机</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="hostList" >
      <el-table-column label="序号" type="index" align="center">
        <template slot-scope="scope">
          <span>{{scope.$index+(queryParams.pageNo-1)*queryParams.pageSize+1}}</span>
        </template>
      </el-table-column>
      <el-table-column label="考点" align="center" prop="examPlaceName" />
      <el-table-column label="考试项目" align="center" prop="examProjectName" />
      <el-table-column label="是否在线" align="center" prop="online" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.isOnline" :value="scope.row.online"/>
        </template>
      </el-table-column>
      <el-table-column label="考生数据版本" align="center" prop="candidateVer" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:host:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加项目主机对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="UID：" prop="uid">
          <el-input v-model="form.uid" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="设备名称：" prop="name">
          <el-input v-model="form.name" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="通讯地址：" prop="address">
          <el-input v-model="form.address" placeholder="请输入" maxlength="16" minlength="16" show-word-limit="true"/>
        </el-form-item>
        <el-form-item label="考点" prop="examPlaceId">
          <el-select v-model="form.examPlaceId" placeholder="请选择" clearable size="small" class="selectWidth">
            <el-option
              v-for="dict in examPlaceIds"
              :key="dict.id"
              :label="dict.name"
              :value="dict.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="项目" prop="examProjectId">
          <el-select v-model="form.examProjectId" placeholder="请选择" clearable size="small" class="selectWidth">
            <el-option
              v-for="dict in projectIdList"
              :key="dict.id"
              :label="dict.name"
              :value="dict.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
    import { listHost, getExamPlaces, getExamProjects, addHost, delHost } from "@/api/business/examHost/examHost";

    export default {
        name: "Host",
        dicts: ['isOnline'],
        data() {
            return {
                // 遮罩层
                loading: true,
                // 导出遮罩层
                exportLoading: false,
                // 选中数组
                ids: [],
                // 非单个禁用
                single: true,
                // 非多个禁用
                multiple: true,
                // 显示搜索条件
                showSearch: true,
                // 总条数
                total: 0,
                // 项目主机表格数据
                hostList: [],
                // 弹出层标题
                title: "",
                // 是否显示弹出层
                open: false,
                // 查询参数
                queryParams: {
                    pageNo: 1,
                    pageSize: 10,
                    main:"",
                    type:""
                },
                // 表单参数
                form: {},
                // 表单校验
                rules: {
                    examPlaceId: [
                        { required: true, message: "请选择考点", trigger: "blur" }
                    ],
                    examProjectId: [
                        { required: true, message: "请选择项目", trigger: "blur" }
                    ],
                    address: [
                        { required: true, message: "通讯地址必须填写", trigger: "blur" },
                        { pattern: /^[\u4e00-\u9fa5_a-zA-Z0-9_]{16,16}/, message: '请输入16位通讯地址' }
                    ],
                    name: [
                        { required: true, message: "设备名称必须填写", trigger: "blur" }
                    ],
                    uid: [
                        { required: true, message: "UID必须填写", trigger: "blur"  }
                    ],
                },
                //考点列表
                examPlaceIds: [],
                //项目列表
                projectIdList:[]
            };
        },
        created() {
            this.getList();
            getExamPlaces().then(response => {
                this.examPlaceIds = response.data;
            });
            getExamProjects().then(response => {
                this.projectIdList = response.data;
            });
        },
        methods: {
            /** 查询项目主机列表 */
            getList() {
                this.loading = true;
                listHost(this.queryParams).then(response => {
                    const records = response.data.records;
                    this.hostList = records;
                    for (var i = 0; i <= records.length; i = i + 1) {
                        if (records[i] == null) {
                            break
                        }
                        this.hostList[i].online = records[i].online+'';
                    }
                    this.total = response.data.totalCount;
                    this.loading = false;
                });
            },
            // 取消按钮
            cancel() {
                this.open = false;
                this.reset();
            },
            // 表单重置
            reset() {
                this.form = {
                    examPlaceId: null,
                    examProjectId: null,
                    remark: null,
                    uid:null,
                    address:null,
                    name:null
                };
                this.resetForm("form");
            },
            /** 搜索按钮操作 */
            handleQuery() {
                this.queryParams.pageNo = 1;
                this.getList();
            },
            /** 重置按钮操作 */
            resetQuery() {
                this.resetForm("queryForm");
                this.handleQuery();
            },
            /** 新增按钮操作 */
            handleAdd() {
                this.reset();
                this.open = true;
                this.title = "添加主机";
            },
            /** 提交按钮 */
            submitForm() {
                this.$refs["form"].validate(valid => {
                    if (valid) {
                        addHost(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                });
            },
            /** 删除按钮操作 */
            handleDelete(row) {
                const ids = row.id || this.ids;
                this.$modal.confirm('是否确认删除项目主机编号为"' + ids + '"的数据项？').then(function() {
                    return delHost(ids);
                }).then(() => {
                    this.getList();
                    this.$modal.msgSuccess("删除成功");
                }).catch(() => {});
            }
        }
    };
</script>
